/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mapdbtest;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.web.HTMLEditor;

/**
 * FXML Controller class
 *
 * @author Arttu
 */
public class NoteEditViewController implements Initializable {
    @FXML
    private TextField headerField;
    @FXML
    private HTMLEditor noteEdit;
    @FXML
    private Button cancelButton;
    @FXML
    private Button saveButton;

    private ObservableNote originalNote;
    private NoteEditCallback noteEditCallback;
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void handleCancelButtonAction(ActionEvent event) {
        this.noteEditCallback.noteEditCanceled(originalNote);
        ((Node)(event.getSource())).getScene().getWindow().hide();
    }

    @FXML
    private void handleSaveButtonAction(ActionEvent event) {
        ObservableNote newNode = new ObservableNote();
        newNode.setId(originalNote.getId());
        newNode.setHeading(headerField.getText());
        newNode.setNote(noteEdit.getHtmlText());
        
        this.noteEditCallback.noteEditFinished(newNode);
        
        ((Node)(event.getSource())).getScene().getWindow().hide();
    }

    void setNote(ObservableNote note) {
        
        this.originalNote = note;
        
        headerField.setText(note.getHeading());
        noteEdit.setHtmlText(note.getNote());
        
    }
    
    public void setCallback(NoteEditCallback callback) {
        this.noteEditCallback = callback;
    }

    public interface NoteEditCallback {

        public void noteEditFinished(ObservableNote note);
        public void noteEditCanceled(ObservableNote note);
        
    }
    
}
